//
//  RCTopListTypeDataModel.m
//  RocketProject
//
//  Created by Wu Chengwei on 13-9-26.
//  Copyright (c) 2013年 Rocket. All rights reserved.
//

#import "RCTopListTypeDataModel.h"
#import "FrameworkRelativeDefines.h"
#import "NSArray+EX.h"

@implementation RCTopListTypeItem

@synthesize typeID = _typeID;
@synthesize typeName = _typeName;

-(id)initWithDictionary:(NSDictionary *)dict{
    self = [super init];
    if (self) {
        self.typeID = [dict stringValueForKey:@"id" defaultValue:nil operation:NSStringOperationTypeTrim];
        self.typeName = [dict stringValueForKey:@"name" defaultValue:nil operation:NSStringOperationTypeTrim];
    }
    return self;
}

-(void)dealloc{
    self.typeID = nil;
    self.typeName = nil;
    [super dealloc];
}

@end

@implementation RCTopListTypeCategoryItem

@synthesize categoryID = _categoryID;
@synthesize categoryName = _categoryName;
@synthesize typeItemArray = _typeItemArray;

-(id)init{
    self = [super init];
    if (self) {
        _categoryID = nil;
        _categoryName = nil;
        _typeItemArray = nil;
    }
    return self;
}

-(void)dealloc{
    self.categoryID = nil;
    self.categoryName = nil;
    self.typeItemArray = nil;
    [super dealloc];
}

+(id)parseDataFromJSON:(NSDictionary *)data{
    if (nil == data) {
        return nil;
    }
    RCTopListTypeCategoryItem *retObject = nil;
    retObject = [[RCTopListTypeCategoryItem alloc]init];
    if (nil == retObject) {
        return nil;
    }
    retObject.categoryID = [data stringValueForKey:@"id" defaultValue:nil operation:NSStringOperationTypeTrim];
    retObject.categoryName = [data stringValueForKey:@"name" defaultValue:nil operation:NSStringOperationTypeTrim];
    NSArray *typeItemDictArray = [data arrayValueForKey:@"categories" defaultValue:nil];
    if (typeItemDictArray != nil && [typeItemDictArray count] > 0) {
        NSMutableArray *tempArray = [[NSMutableArray alloc]initWithCapacity:[typeItemDictArray count]];
        for (int i = 0; i < [typeItemDictArray count]; i++) {
            NSDictionary *itemDict = [typeItemDictArray safeObjectAtIndex:i];
            if (nil == itemDict) {
                continue;
            }
            RCTopListTypeItem *item = [[RCTopListTypeItem alloc]initWithDictionary:itemDict];
            [tempArray addObject:item];
            [item release];
        }
        retObject.typeItemArray = [NSArray arrayWithArray:tempArray];
        [tempArray release];
    }
    return [retObject autorelease];
}

@end

@implementation RCTopListTypeDataModel

@synthesize categoryListArray = _categoryListArray;

-(id)init{
    self = [super init];
    if (self) {
        _categoryListArray = nil;
    }
    return self;
}

-(void)dealloc{
    self.categoryListArray = nil;
    [super dealloc];
}

+(id)parseDataFromJSON:(NSDictionary *)data{
    if (nil == data) {
        return nil;
    }
    NSArray *dictArray = [data arrayValueForKey:@"data" defaultValue:nil];
    if (nil == dictArray) {
        return nil;
    }
    RCTopListTypeDataModel *retObject = nil;
    retObject = [[RCTopListTypeDataModel alloc]init];
    if (nil == retObject) {
        return nil;
    }
    NSMutableArray *tempArray = [[NSMutableArray alloc]initWithCapacity:[dictArray count]];
    for (int i=0; i < [dictArray count]; i++) {
        NSDictionary *itemDict = [dictArray safeObjectAtIndex:i];
        if (nil == itemDict) {
            continue;
        }
        RCTopListTypeCategoryItem *item = [RCTopListTypeCategoryItem parseDataFromJSON:itemDict];
        if (nil == item) {
            continue;
        }
        [tempArray addObject:item];
    }
    retObject.categoryListArray = [NSArray arrayWithArray:tempArray];
    [tempArray release];
    return [retObject autorelease];
}

@end

@implementation RCTopListTypeMoreDataModel

@synthesize typeListArray = _typeListArray;

-(id)init{
    self = [super init];
    if (self) {
        _typeListArray = nil;
    }
    return self;
}

-(void)dealloc{
    self.typeListArray = nil;
    [super dealloc];
}

+(id)parseDataFromJSON:(NSDictionary *)data{
    if (nil == data) {
        return nil;
    }
    NSArray *dictArray = [data arrayValueForKey:@"data" defaultValue:nil];
    if (nil == dictArray) {
        return nil;
    }
    
    RCTopListTypeMoreDataModel *retObject = nil;
    retObject = [[RCTopListTypeMoreDataModel alloc]init];
    if (nil == retObject) {
        return nil;
    }
    NSMutableArray *tempArray = [[NSMutableArray alloc]initWithCapacity:[dictArray count]];
    for (int i=0; i < [dictArray count]; i++) {
        NSDictionary *itemDict = [dictArray safeObjectAtIndex:i];
        if (nil == itemDict) {
            continue;
        }
       RCTopListTypeItem *item = [[RCTopListTypeItem alloc]initWithDictionary:itemDict];
        if (nil == item) {
            continue;
        }
        [tempArray addObject:item];
        [item release];
    }
    retObject.typeListArray = [NSArray arrayWithArray:tempArray];
    [tempArray release];
    
    return [retObject autorelease];
}

@end
